<?php

namespace Boosen\Admin\Contents;

use Encore\Admin\Admin;
use Encore\Admin\Auth\Database\Menu;
use Encore\Admin\Extension;
use Illuminate\Routing\Router;

class Contents extends Extension
{
    /**
     * Bootstrap this package.
     *
     * @return void
     */
    public static function boot()
    {
        static::registerRoutes();

        Admin::extend('contents', __CLASS__);
    }

    /**
     * Register routes for laravel-admin.
     *
     * @return void
     */
    public static function registerRoutes()
    {
        parent::routes(function ($router) {
            /* @var \Illuminate\Routing\Router $router */
            $router->resource(
                'cms/' . config('contents.routes.categroy', 'categroies'),
                'Boosen\Admin\Contents\Controllers\CategoryController'
            );
            $router->resource(
                'cms/' . config('contents.routes.post', 'posts'),
                'Boosen\Admin\Contents\Controllers\PostController'
            );
            $router->resource(
                'cms/' . config('contents.routes.page', 'pages'),
                'Boosen\Admin\Contents\Controllers\PageController'
            );

            // $router->resource('{mid}/' . config('contents.routes.menu_items', 'menu_items'), 'Boosen\Admin\Contents\Controllers\MenuItemController', ['except' => ['create']]);
        });
    }

    public static function import()
    {
        $lastOrder = Menu::max('order');

        $root = [
            'parent_id' => 0,
            'order'     => $lastOrder++,
            'title'     => 'Contents',
            'icon'      => 'fa-file-text',
            'uri'       => '',
        ];

        $root = Menu::create($root);

        $menus = [
            [
                'title'     => 'Categroy',
                'icon'      => 'fa-keyboard-o',
                'uri'       => 'cms/categroies',
            ],
            [
                'title'     => 'Posts',
                'icon'      => 'fa-keyboard-o',
                'uri'       => 'cms/posts',
            ],
            [
                'title'     => 'Pages',
                'icon'      => 'fa-keyboard-o',
                'uri'       => 'cms/pages',
            ]
        ];

        foreach ($menus as $menu) {
            $menu['parent_id'] = $root->id;
            $menu['order'] = $lastOrder++;

            Menu::create($menu);
        }

        parent::createPermission('Contents Management', 'ext.cms', 'cms/*');
    }
}
